using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace IceAge.Trading
{
    public partial class PlaceOrderX : Form
    {
       
        public PlaceOrderX()
        {
            InitializeComponent();
        }
        private object[] parameters = null;
        private void PlaceOrderX_Load(object sender, EventArgs e)
        {
            cboSide.DataSource = CommonLibrary.XML_DATA.Tables["ComboData"];
            cboSide.ValueMember = "ValueMember";
            cboSide.DisplayMember = "VnDisplayMember";
            cboSide.SelectedIndex = cboSide.Items.Count - 1;
            parameters = (object[])this.Tag;
            txtSymbol.Text = parameters[0].ToString().Trim();            
        }

        private bool Validate_DataInput()
        {
            if (txtSymbol.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Ma chung khoan khong duoc phep de trong!",
               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (nbrVolume.Value == 0)
            {
                MessageBox.Show("Khoi luong giao dich phai lon hon 0!",
               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cboPrice.Text != "ATO" && cboPrice.Text != "ATC")
            {
                decimal price = 0;
                if (!decimal.TryParse(cboPrice.Text,out price))
                {
                    MessageBox.Show("Gia giao dich phai la ATO, ATC hoac dang so!",
                   this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            if (txtFirmCode.Text.Trim().Length != 4
                || txtClientId.Text.Trim().Length != 6)
            {
                MessageBox.Show("So tai khoan giao dich cua khach hang khong hop le",
                   this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if (!Validate_DataInput())
                return;
            if (((DataRowView)cboSide.SelectedItem)["ValueMember"].ToString() == "B")
            {
                if (MessageBox.Show("Dat lenh mua " + nbrVolume.Value.ToString("###,###,###,###") + txtSymbol.Text + " gia " + cboPrice.Text
                    + " tai khoan " + txtFirmCode.Text + txtClientId.Text + "?",
                       this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;
            }
            else
            {
                if (MessageBox.Show("Dat lenh ban " + nbrVolume.Value.ToString("###,###,###,###") + txtSymbol.Text + " gia " + cboPrice.Text
                    + " tai khoan " + txtFirmCode.Text + txtClientId.Text + "?",
                       this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    return;
            }
            //CenturyFox.Service data_access = new CenturyFox.Service();
            int error_code =Convert.ToInt32( CommonLibrary.ExecuteScalar("0302",
                new object[8] {CommonLibrary.TRAN_DATE, DateTime.Now.ToString("HH:mm:ss"), txtFirmCode.Text + txtClientId.Text,
                    ((DataRowView) cboSide.SelectedItem)["ValueMember"].ToString(), txtSymbol.Text, nbrVolume.Value , cboPrice.Text, CommonLibrary.USER_NAME}));
            switch (error_code)
            {
                case 0:
                    if (((DataRowView)cboSide.SelectedItem)["ValueMember"].ToString() == "B")
                        MessageBox.Show("Dat lenh mua " + nbrVolume.Value.ToString("###,###,###,###") + txtSymbol.Text + " gia " + cboPrice.Text
                            + " tai khoan " + txtFirmCode.Text + txtClientId.Text + " thanh cong",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        MessageBox.Show("Dat lenh ban " + nbrVolume.Value.ToString("###,###,###,###") + txtSymbol.Text + " gia " + cboPrice.Text
                            + " tai khoan " + txtFirmCode.Text + txtClientId.Text + " thanh cong",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -2:
                    MessageBox.Show("Chung khoan: " + txtSymbol.Text + " khong ton tai",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -3:
                    MessageBox.Show("Tai khoan: " + txtFirmCode.Text + txtClientId.Text + " khong du tien dat lenh",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -4:
                    MessageBox.Show("Tai khoan: " + txtFirmCode.Text + txtClientId.Text + " khong du chung khoan dat lenh",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -5:
                    MessageBox.Show("Tai khoan: " + txtFirmCode.Text + txtClientId.Text +  " vuot qua gioi han dat lenh",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;

                case -6:
                    MessageBox.Show("Tai khoan: " + txtFirmCode.Text + txtClientId.Text + " khong ton tai",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -7:
                    MessageBox.Show("Gia: " + cboPrice.Text + " khong nam trong gioi hang giua gia tran va gia san",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -8:
                    MessageBox.Show("Khoi luong: " + nbrVolume.Value + " khong dung lot giao dich",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -9:
                    MessageBox.Show("Khoi luong: " + nbrVolume.Value + " vuot qua gioi han dat lenh tai HOSE",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -10:
                    MessageBox.Show("Gia: " + cboPrice.Text + " phai chan 100 VND",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -11:
                    MessageBox.Show("Gia: " + cboPrice.Text + " phai chan 500 VND",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -12:
                    MessageBox.Show("Gia: " + cboPrice.Text + " phai chan 1000 VND",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -13:
                    MessageBox.Show("Chi cho phep dat lenh gia ATO, ATC tren HOSE",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -14:
                    MessageBox.Show("Khoi luong: " + nbrVolume.Value + " khong dung lot tren HNX",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -16:
                    MessageBox.Show("Khong ket noi duoc voi may chu quan ly lenh",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                case -18:
                    MessageBox.Show("Tai khoan: " + txtFirmCode.Text + txtClientId.Text + " dinh chi mua",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
                
                default:
                    MessageBox.Show("Khong khoi tao duoc lenh dat",
                               this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    break;
            }
        }

        private void txtSymbol_Validated(object sender, EventArgs e)
        {
            txtSymbol.Text = txtSymbol.Text.Trim().ToUpper();
           DataRow[] rows = CommonLibrary.STOCK_INFO.Tables[0]
               .Select("SYMBOL = '" + txtSymbol.Text + "'");
           cboPrice.Items.Clear();
           if (rows.Length > 0)
           {
               cboPrice.Items.Add(Convert.ToDecimal(rows[0]["CEILING"]).ToString(global::IceAge.Properties.Settings.Default.CurrencyFormat));
               cboPrice.Items.Add(Convert.ToDecimal(rows[0]["FLOOR"]).ToString(global::IceAge.Properties.Settings.Default.CurrencyFormat));               
           }           
        }

        private void txtFirmCode_Validated(object sender, EventArgs e)
        {
            txtFirmCode.Text = txtFirmCode.Text.Trim().ToUpper();
        }

        private void cboPrice_Validated(object sender, EventArgs e)
        {            
            if (cboPrice.Text.Trim().ToUpper() == "ATO"
                || cboPrice.Text.Trim().ToUpper() == "ATC")
                cboPrice.Text = cboPrice.Text.Trim().ToUpper();
            CalcOrderValue();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtClientId.Clear();
            txtSymbol.Clear();
            cboPrice.Text = string.Empty;
            nbrVolume.Value = 0;
        }

        private void CalcOrderValue()
        {
            decimal quote_price =0;
            if (cboPrice.Text == "ATO" || cboPrice.Text == "ATC")
            {
                quote_price = 0;
            }
            else
            {
                if (!decimal.TryParse(cboPrice.Text, out quote_price))
                {
                    quote_price = 0;
                }
            }
            lblAmount.Text = Convert.ToDecimal(nbrVolume.Value * quote_price)
                .ToString("###,###,###,###");
        }
        private void nbrVolume_Validated(object sender, EventArgs e)
        {
            CalcOrderValue();
        }

        private void cboSide_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((DataRowView)cboSide.SelectedItem)["ValueMember"].ToString() == "S")
                panel1.BackColor = Color.Red;
            else
                panel1.BackColor = Color.Blue;
        }
    }
}